﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecommerce.Model;

namespace Ecommerce.Web.Admin.Paginas
{
    public partial class ListarCupom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                CarregarCupons();
        }

        private void CarregarCupons()
        {
            List<tb_cup_cupom> cupons = CupomModel.Instance.Listar();
            grvCupons.DataSource = cupons;
            grvCupons.DataBind();
        }

        protected void grvCupons_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var cupom = (tb_cup_cupom)e.Row.DataItem;
                e.Row.Cells[0].Text = cupom.cup_idt_cupom.ToString();
                e.Row.Cells[1].Text = cupom.cup_dat_criacao.ToString();

                e.Row.Cells[2].Text = cupom.cup_cod_cupom;
                e.Row.Cells[3].Text = cupom.cup_dat_inicio_validade.ToString();
                e.Row.Cells[4].Text = cupom.cup_dat_fim_validade.ToString();
                e.Row.Cells[5].Text = cupom.cup_val_desconto.ToString();
                e.Row.Cells[6].Text = cupom.rev_idt_revenda.ToString();

                if (cupom.cup_flg_cupom_ativo)
                    e.Row.Cells[7].Text = "Sim";
                else
                    e.Row.Cells[7].Text = "Não";

                LinkButton lkbEditar = (LinkButton)e.Row.FindControl("lkbEditar");
                lkbEditar.Text = "Editar";
                lkbEditar.PostBackUrl = "GerenciarCupom.aspx?CId=" + cupom.cup_idt_cupom.ToString() + "&Editar=true";
            }
        }

        protected void grvCupons_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            CarregarCupons();
            grvCupons.PageIndex = e.NewPageIndex;
            grvCupons.DataBind();
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {

        }
    }
}